home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
Issue51
/
Clinic
/
RunCmd.pas
< prev
Wrap
Pascal/Delphi Source File
|
1999-09-09
|
2KB
|
69 lines
unit RunCmd;
interface
procedure RunCommand(const Cmd, Params: String);
//Extended version of RunCommand which can handle file associations
procedure RunCommandEx(const Cmd, Params: String);
implementation
uses
SysUtils, Forms, ShellAPI, Windows;
procedure RunCommand(const Cmd, Params: String);
var
SI: TStartupInfo;
PI: TProcessInformation;
CmdLine: String;
begin
//Fill record with zero byte values
FillChar(SI, SizeOf(SI), 0);
//Set mandatory record field
SI.cb := SizeOf(SI);
//Ensure Windows mouse cursor reflects launch progress
SI.dwFlags := StartF_ForceOnFeedback;
//Set up command line
CmdLine := Cmd;
if Length(Params) > 0 then
CmdLine := CmdLine + #32 + Params;
//Try and launch child process. Raise exception on failure
Win32Check(
CreateProcess(
nil, PChar(CmdLine), nil, nil, False, 0, nil, nil, SI, PI));
//Wait until process has started its main message loop
WaitForInputIdle(PI.hProcess, Infinite);
//Close process and thread handles
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
//Extended version of RunCommand which can handle file associations
procedure RunCommandEx(const Cmd, Params: String);
var
SEI: TShellExecuteInfo;
begin
//Fill record with zero byte values
FillChar(SEI, SizeOf(SEI), 0);
//Set mandatory record field
SEI.cbSize := SizeOf(SEI);
//Ask for an open process handle
SEI.fMask := see_Mask_NoCloseProcess;
//Tell API which window any error dialogs should be modal to
SEI.Wnd := Application.Handle;
//Set up command line
SEI.lpFile := PChar(Cmd);
if Length(Params) > 0 then
SEI.lpParameters := PChar(Params);
SEI.nShow := sw_ShowNormal;
//Try and launch child process. Raise exception on failure
if not ShellExecuteEx(@SEI) then
Exit;
//Wait until process has started its main message loop
WaitForInputIdle(SEI.hProcess, Infinite);
//Close process handle
CloseHandle(SEI.hProcess);
end;
end.